// pages/songDetail/songDetail.js
import pubsub from 'pubsub-js'
import moment from 'moment'
import request from '../../utils/request.js'
const appInstance=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay:false,
    song:{},
    musicId:'',
    musicLink:'',
    currentTime:'00:00',
    durationTime:'',
    currentWidth:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 原生小程序中路由的传参，对长度有限制，如果参数长度过长会自动截取掉
    // console.log(JSON.parse(options.song))
    let musicId=options.musicId
    this.setData({
      musicId
    })
    this.getMusicInfo(musicId)

    if (appInstance.globalData.musicId===musicId && appInstance.globalData.isMusicPlay){
      this.setData({
        isPlay:true
      })
    }
    this.backgroundAudioManager = wx.getBackgroundAudioManager()
    this.backgroundAudioManager.onPlay(()=>{
     this.changePlayState(true)
      appInstance.globalData.musicId = musicId
    })
    this.backgroundAudioManager.onPause(()=>{
      this.changePlayState(false)
    })
    this.backgroundAudioManager.onStop(() => {
     this.changePlayState(false)
    })
    this.backgroundAudioManager.onEnded(() => {
      pubsub.publish('switch', 'next')
      this.setData({
        currentWidth:0,
        currentTime:'00:00'
      })
    })

    this.backgroundAudioManager.onTimeUpdate(()=>{
      let currentTime=moment(this.backgroundAudioManager.currentTime*1000).format('mm:ss')
      let currentWidth = (this.backgroundAudioManager.currentTime /        this.backgroundAudioManager.duration)*450
      this.setData({
        currentTime,
        currentWidth
      })
    })
   
  },
  changePlayState(isPlay){
    this.setData({
      isPlay
    })
    appInstance.globalData.isMusicPlay = isPlay
  },
  async getMusicInfo(musicId){
    let songData = await request('/song/detail',{ids:musicId})
    let durationTime = moment(songData.songs[0].dt).format('mm:ss')
    this.setData({
      song:songData.songs[0],
      durationTime
    })
    wx.setNavigationBarTitle({
      title: this.data.song.name
    })
  },
  handleMusic(){
    let isPlay=!this.data.isPlay
    // this.setData({
    //   isPlay
    // })
    let { musicId } = this.data
    this.musicControl(isPlay,musicId)
  },
  async musicControl(isPlay, musicId){
    
    if(isPlay){
       let musicLinkeData = await request('/song/url', { id: musicId })
       let musicLink = musicLinkeData.data[0].url
      this.backgroundAudioManager.src = musicLink
      this.backgroundAudioManager.title = this.data.song.name
    }else{
      this.backgroundAudioManager.pause()
    }
  },
  
  handleSwitch(e){
    let type =e.currentTarget.id
    this.backgroundAudioManager.stop()
    pubsub.subscribe('musicId', (msg, musicId) => {
      // console.log(musicId)
      this.getMusicInfo(musicId)
      this.musicControl(true,musicId)
      pubsub.unsubscribe('musicId')
    })
    pubsub.publish('switch',type)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})